package com.cg.result;

import com.cg.Exception.CommonErrorCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class ResultBase<T>{
    //正常或异常的编码 String -> 常量类
    private CommonErrorCode code;
    //成功提示文案 或者异常提示文案
    private String message;
    //成功或失败
    private Boolean success;
    //数据
    private T data;
    public static <T> ResultBase<T> success(T data) {
        ResultBase resultBase = new ResultBase();
        resultBase.setCode(CommonErrorCode.SUCCESS);
        resultBase.setSuccess(Boolean.TRUE);
        resultBase.setMessage("成功");
        resultBase.setData(data);
        return resultBase;
    }
    public static ResultBase success() {
        ResultBase resultBase = new ResultBase();
        resultBase.setCode(CommonErrorCode.SUCCESS);
        resultBase.setSuccess(Boolean.TRUE);
        resultBase.setMessage("成功");
        return resultBase;
    }
    public static ResultBase fail() {
        return fail("失败");
    }
    public static ResultBase fail(String message) {
        return fail(CommonErrorCode.FAIL, message);
    }
    public static ResultBase fail(CommonErrorCode code, String message) {
        ResultBase resultBase = new ResultBase();
        resultBase.setCode(code);
        resultBase.setSuccess(Boolean.FALSE);
        resultBase.setMessage(message);
        return resultBase;
    }
    public boolean isSuccess() {
        return this.getCode().equals(CommonErrorCode.SUCCESS);
    }
}
